const http=require('http');
/*HTTP 模块中封装了一个 HTTP 服务器和一个简单的 HTTP 客户端*/
/**
* @Description:httpServer本质,和content-type头信息
* @DescriptionLink: https://blog.csdn.net/weixin_36910300/article/details/80532868  /  https://segmentfault.com/a/1190000022402271
* @DescriptionLink: https://blog.csdn.net/maindek/article/details/74999039
* @author
* @date 2020/8/12
**/
const server=new http.Server();
server.on('request',function (req,res) {
  res.writeHead(200,{
    "content-type":"text/html; charset=utf-8"
  });
  res.write('<h1>这是h1</h1>');
  res.end();
})

setTimeout(function(){ //http1.1中是长连接,所以客户端浏览器必须关闭才能销毁服务
  server.close(function () {
    console.log("server close");
  });
},3000);
//所以如果你想及时关闭一个server，或者重启它。你需要自己管理这些socket连接池。在nodejs中需要自己销毁他们。

//当tcp建立连接时提供的connection 里面有个参数 socket
let sockets = [];
server.on('connection',function (socket) {
  sockets.push(socket);
  socket.once("close",function(){
    sockets.splice(sockets.indexOf(socket),1);
  });
});

function closeServer() {
  sockets.forEach(function (socket) {
    socket.destroy();
  });
  server.close(function () {
    console.log("close server!");
  });
}
server.listen(8003,"localhost",()=>console.log(`server is running in http://localhost:8003`))